package com.qf.controller;

import com.qf.mapper.SubitemMapper;
import com.qf.mapper.TaskplanMapper;
import com.qf.model.*;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ProjectService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/project")
@RestController
public class ProjectController {

    @Autowired
    private ProjectService projectService;



    @RequestMapping("/findAll")
    public List<Project> findAll(){
        List<Project> all = projectService.getAll();
        return all;
    }


    /**
     * project工程列表分页查询+模糊查询
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = projectService.findPage(queryPageBean);
        return pageResult;
    }


    /**
     *新增project工程
     * @param project
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Project project){
        Result result = new Result();
        int i = projectService.addProject(project);
        if (i>0){
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }

    /**
     * 编辑project回显数据
     * @param pid
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("pid") Integer pid ){
        Result result = new Result();
        Project project = projectService.findById(pid);
        boolean b = ObjectUtils.isEmpty(project);
        if (!b){
            result.setData(project);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }

    /**
     * 修改project
     * @param project
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Project project){
        Result result = new Result();
        int i = projectService.edit(project);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除project
     * @param pid
     * @return  result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("pid") Integer pid){
        Result result = new Result();
        int i = projectService.delete(pid);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    @GetMapping("/index")
    public Result index(@RequestParam("pid") int pid){
        Result result = new Result();
        Map<String, Object> index = projectService.index(pid);
        if (ObjectUtils.isNotEmpty(index)){
            result.setFlag(true);
            result.setMessage("success");
            result.setData(index);
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }
    @GetMapping("/cname")
    public Result cname(@RequestParam("pid") int pid){
        Result result = new Result();
        List<Subitem> subitems = projectService.cname(pid);
        if (ObjectUtils.isNotEmpty(subitems)){
            result.setFlag(true);
            result.setMessage("success");
            result.setData(subitems);
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    @GetMapping("/item")
    public Result item(@RequestParam("suid") String suid){
        Result result = new Result();
        List<Item> items = projectService.item(suid);
        if (ObjectUtils.isNotEmpty(items)){
            result.setFlag(true);
            result.setMessage("success");
            result.setData(items);
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
    @GetMapping("/bname")
    public Result bname(@RequestParam("pid") int pid){
        Result result = new Result();
        List<Branch> branches = projectService.bname(pid);
        if (ObjectUtils.isNotEmpty(branches)){
            result.setFlag(true);
            result.setMessage("success");
            result.setData(branches);
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }


    @GetMapping("/taskplan")
    public Result taskplan(@RequestParam("bid") String bid){
        Result result =new Result();
        List<Taskplan> taskplans = projectService.taskplan(bid);
        if (ObjectUtils.isNotEmpty(taskplans)){
            result.setFlag(true);
            result.setMessage("success");
            result.setData(taskplans);
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

}
